package com.cqywc.cjadmin.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.hibernate.validator.HibernateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.validation.beanvalidation.LocalValidatorFactoryBean;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.cqywc.cjadmin.pojo.Menu;
import com.cqywc.cjadmin.service.MenuService;
import com.cqywc.cjadmin.service.ReturnSerivice;
import com.cqywc.cjadmin.validate.MenuAdd;
import com.cqywc.cjadmin.validate.MenuEdit;

@Controller
@RequestMapping("admin/menu/")
public class AdminMenuController {
	
	@Autowired
	private MenuService menuService;
	@Autowired
	private ReturnSerivice returnSerivice;
	
	@RequestMapping("lists")
	public String lists() {
		System.out.println(123);
		return "admin/menu/lists";
	}
	
	/**
	 * 返回菜单列表json数据
	 * @return
	 */
	@RequestMapping("listsJson")
	public @ResponseBody ReturnSerivice listsJson(){
		List<Menu> menulists = menuService.queryList();
		returnSerivice.setCode(0);
		returnSerivice.setMsg("ok");
		Map<String, Object> map=new HashMap<String, Object>();
		map.put("menuLists", menulists);
		returnSerivice.setData(map);
		return returnSerivice;
	}
	/**
	 * 新增菜单
	 * @return
	 */
	@RequestMapping("add")
	public ModelAndView add(Integer pid) {
		
		ModelAndView modelAndView =new ModelAndView();
		List<Menu> menulists = menuService.queryList();
		modelAndView.addObject("menuLists", menulists);
		modelAndView.addObject("pid", pid);
		modelAndView.setViewName("admin/menu/add");
		return modelAndView;
	}
	/**
	 * 新增菜单提交
	 * @param menu
	 * @param result
	 * @return
	 * @throws Exception
	 */
	@ResponseBody
	@RequestMapping("addMenuSave")
	public ReturnSerivice addMenuSave(@Validated(value= {MenuAdd.class}) Menu menu,BindingResult result) throws Exception {
	
		if(result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			String msgString =allErrors.get(0).getDefaultMessage();
				throw new Exception(msgString);
		}
		menuService.saveMenu(menu);
		returnSerivice.setCode(0);
		returnSerivice.setMsg("ok");
		return returnSerivice;
	}
	/**
	 * 修改菜单页面
	 * @param id
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("editMenu")
	public ModelAndView editMenu(Integer id) throws Exception {
		ModelAndView modelAndView =new ModelAndView();
	 	Menu menu= menuService.queryById(id);
	 	modelAndView.addObject("menu", menu);
	 	modelAndView.setViewName("admin/menu/editMenu");
		return modelAndView;
	}
	@RequestMapping("editMenuSave")
	@ResponseBody
	public ReturnSerivice editMenuSave(@Validated(value= {MenuEdit.class}) Menu menu,BindingResult result) throws Exception {

		if(result.hasErrors()) {
			List<ObjectError> allErrors = result.getAllErrors();
			String msgString =allErrors.get(0).getDefaultMessage();
				throw new Exception(msgString);
		}
		menuService.updateMenu(menu);
		returnSerivice.setCode(0);
		returnSerivice.setMsg("ok");
		return returnSerivice;
		
	}
	
	
}
